package com.neu.architecture.controller;

import com.neu.architecture.util.CommonPage;
import com.neu.architecture.util.CommonResult;
import com.neu.architecture.dto.InfoParam;
import com.neu.architecture.dto.InfoQueryParam;
import com.neu.architecture.model.Info;
import com.neu.architecture.service.ArchitectureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/info")
public class InfoController {

    @Autowired
    ArchitectureService architectureService;

    //@GetMapping("/fetchList")
    //public CommonResult<CommonPage<Info>> fetchList(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
    //                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
    //    // 根据页大小和页码获取建筑信息列表
    //    List<Info> architectureList = architectureService.fetchList(pageSize, pageNum);
    //    return CommonResult.success(CommonPage.restPage(pageSize, pageNum, architectureList));
    //}

    @GetMapping("/searchArchis")
    public CommonResult<CommonPage<Info>> searchArchis(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   InfoQueryParam param) {
        System.out.println("param =>" + param);
        List<Info> architectureList = architectureService.searchArchis(pageSize, pageNum, param);
        return CommonResult.success(CommonPage.restPage(pageSize, pageNum, architectureList));
    }

    @DeleteMapping("/deleteArchi/{id}")
    public CommonResult deleteArchi(@PathVariable long id) {
        int count = architectureService.deleteArchi(id);
        if (count > 0) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed("删除失败!");
        }
    }

    @GetMapping("/getArchi/{id}")
    public CommonResult<CommonPage<Info>> getArchi(@PathVariable long id) {
        List<Info> architectureList = architectureService.getArchiById(id);
        return CommonResult.success(CommonPage.restPage(architectureList));
    }

    @PutMapping("/updateArchi/{id}")
    public CommonResult updateArchi(@PathVariable long id, @RequestBody InfoParam param) {
        int count = architectureService.updateArchi(param, id);
        if (count > 0) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed("更新失败!");
        }
    }

    @PostMapping("/addArchi")
    public CommonResult addArchi(@RequestBody Info info) {
        System.out.println(info);
        int count = architectureService.addArchi(info);
        if (count > 0) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed("更新失败!");
        }
    }
}
